package com.shop2cn.iapi.wxcomponent.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.shop2cn.iapi.wxcomponent.parameter.GetMchAppDetailBean;
import com.shop2cn.iapi.wxcomponent.parameter.ReleaseBean;
import com.shop2cn.iapi.wxcomponent.service.GetMchAppDetailCall;
import com.shop2cn.iapi.wxcomponent.service.ReleaseCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.WxComponentService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.DGUserInfo;
import com.ymttest.database.model.Mchapp;
import com.ymttest.database.model.mch_info;
import com.ymttest.database.sqlwapper.DGUserWapper;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.utils.YMTDateUtil;
/**
*查询小程序详情
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetMchAppDetail{
	private static GetMchAppDetailBean getmchappdetailBean;
	private static GetMchAppDetailCall getmchappdetailCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询小程序详情");
	}

	@Before
	public void caseUp() {
		getmchappdetailBean=new GetMchAppDetailBean();
		getmchappdetailCall=new GetMchAppDetailCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getmchappdetailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMchAppDetail_001() {
		Logger.start(true,"查询小程序详情");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			String timestamp=String.valueOf(YMTDateUtil.getDate().getTime()/1000);
			WxComponentService.deleteMchinfo(mchid);
			String appid = WxComponentService.ConfirmAuthorization(mchid);
			WxComponentService.AuthorizationCallBack(appid, "authorized");
			WxComponentService.ConfirmAuditMchApp(mchid);
			WxComponentService.Remark("mark", mchid);
			getmchappdetailBean.setMchId(mchid);
			getmchappdetailCall.setData(getmchappdetailBean);
			getmchappdetailCall.callService();		
			Logger.debug(getmchappdetailCall.getStatusCode());
			Logger.verifyEquals("200", getmchappdetailCall.getString("code"), "验证返回code");
			JsonObject ja = getmchappdetailCall.getJsonObject().get("data").getAsJsonObject();
			DefGodMchWapper DefGodMchWapper=new DefGodMchWapper();
			Mchapp defmchapp = DefGodMchWapper.selectMchappByExample(mchid).get(0);
			List<mch_info> mchlist = DefGodMchWapper.selectMchInfo(defmchapp.getMchId());
			if(mchlist!=null&&!mchlist.isEmpty()) {
				Logger.verifyEquals(mchlist.get(0).getMchTitle(),ja.get("mchName").getAsString(), "验证mchName");
			}
			List<DGUserInfo> userlist = new DGUserWapper().selectDGUserInfo(defmchapp.getMchId());
			if(userlist!=null&&!userlist.isEmpty()) {
				Logger.verifyEquals(userlist.get(0).getMobile(),ja.get("mobile").getAsString(), "验证mobile");
			}
			Logger.verifyEquals(ja.get("appStatus").getAsInt(),defmchapp.getAppStatus(), "验证返回appStatus");
			Logger.verifyEquals(ja.get("payChannelStatus").getAsInt(),defmchapp.getPayChannelStatus(), "验证返回payChannelStatus");
			Logger.verifyEquals(ja.get("principalName").getAsString(),defmchapp.getPrincipalName(), "验证返回principalName");
			Logger.verifyEquals(ja.get("mark").getAsString(),defmchapp.getRemark(), "验证返回mark");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetMchAppDetail_002() {
		Logger.start(true,"不输入mchid失败");
		try {
			getmchappdetailCall.setData(getmchappdetailBean);
			getmchappdetailCall.callService();			
			Logger.verifyEquals("400", getmchappdetailCall.getString("code"), "验证返回code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetMchAppDetail_003() {
		Logger.start(true,"输入错误MchId失败");
		try {
			getmchappdetailBean.setMchId(-1);
			getmchappdetailCall.setData(getmchappdetailBean);
			getmchappdetailCall.callService();			
			Logger.verifyEquals("400", getmchappdetailCall.getString("code"), "验证返回code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMchAppDetail_004() {
		Logger.start(true,"小程序发布，获取发布版本号");
		try {
			Mchapp mchapp = new DefGodMchWapper().selectMchappByPublish(10).get(0);
			int mchid=mchapp.getMchId();
			DefGodMchWapper DefGodMchWapper=new DefGodMchWapper();
			
			getmchappdetailBean.setMchId(mchid);
			getmchappdetailCall.setData(getmchappdetailBean);
			getmchappdetailCall.callService();		
			Logger.debug(getmchappdetailCall.getStatusCode());
			Logger.verifyEquals("200", getmchappdetailCall.getString("code"), "验证返回code");
			JsonObject ja = getmchappdetailCall.getJsonObject().get("data").getAsJsonObject();
			Mchapp defmchapp = DefGodMchWapper.selectMchappByExample(mchid).get(0);
			List<mch_info> mchlist = DefGodMchWapper.selectMchInfo(defmchapp.getMchId());
			if(mchlist!=null&&!mchlist.isEmpty()) {
				Logger.verifyEquals(mchlist.get(0).getMchTitle(),ja.get("mchName").getAsString(), "验证mchName");
			}
			List<DGUserInfo> userlist = new DGUserWapper().selectDGUserInfo(defmchapp.getMchId());
			if(userlist!=null&&!userlist.isEmpty()) {
				Logger.verifyEquals(userlist.get(0).getMobile(),ja.get("mobile").getAsString(), "验证mobile");
			}
			Logger.verifyEquals(ja.get("appStatus").getAsInt(),defmchapp.getAppStatus(), "验证返回appStatus");
			Logger.verifyEquals(ja.get("payChannelStatus").getAsInt(),defmchapp.getPayChannelStatus(), "验证返回payChannelStatus");
			Logger.verifyEquals(ja.get("principalName").getAsString(),defmchapp.getPrincipalName(), "验证返回principalName");
			Logger.verifyEquals(ja.get("mark").getAsString(),defmchapp.getRemark(), "验证返回mark");
			Logger.verifyEquals(ja.get("appVersion").getAsString(),"autotest1", "验证发布的版本");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}